//
//  RCDiscountSubView.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-6-29.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCDiscountSubView.h"
#import "AppDelegate.h"
#import "NSArray+EX.h"
#import "NSString+NSString_Ex_.h"
#import "UIColor-Expanded.h"
#import "TravelException.h"
#import "BMapKit.h"
#import "GloableEngineService.h"
#import "NCLocationController.h"
#import "CommonDefines.h"
#import "FrameworkRelativeDefines.h"
#import "IManager+ModuleToplist.h"
#import "RCMerchantListCell.h"
#import "RCTopListTypeDataModel.h"
#import "RCRecommendFoodTypeView.h"

#define kMaxRequestCount                  15

#define kButtonImageFile                  @"rc_discount_subView_button.png"
#define kNoLocationImageFile              @"rc_discount_subView_noContent.png"
#define kNoNetWrokImageFile               @"rc_discount_subView_noNetWork.png"
#define kNoNetworkButtonImage             @"rc_album_my_edit_button_back_normal.png"
#define kNoNetworkPressButtonImage        @"rc_album_my_edit_button_back_highlight.png"

#define kMinLatitudeOffset                 0.001
#define kMinLongitudeOffset                0.002

#define kTableViewNormal                   201
#define kTableViewMoreType                 202

#define kBGimageFile        @"image_searchAllCity_cell_bg.png"
#define kLineImageFile      @"image_line_type3.png"

@interface RCDiscountSubView()


@property(nonatomic, assign) BOOL isRefreshData;
@property(nonatomic, assign) int totalCount;
@property(nonatomic, retain) UIView *wrongDataView;
@property(nonatomic, retain) UIImageView *wrongDataImageView;
@property(nonatomic, retain) UILabel *wrongDataLabel;
@property(nonatomic, assign) int64_t requestMoreTypeID;
@property(nonatomic, retain) RCTopListTypeMoreDataModel *topMoreDataModel;
@property(nonatomic, assign) BOOL hasLoadBaseTable;

@end

@implementation RCDiscountSubView

@synthesize delegate = _delegate;
@synthesize requestID = _requestID;
@synthesize typeID = _typeID;
@synthesize discountType = _discountType;
@synthesize isRefreshData = _isRefreshData;
@synthesize currentOffset = _currentOffset;
@synthesize currentReturnNumber = _currentReturnNumber;
@synthesize totalCount = _totalCount;
@synthesize discountDataInfo = _discountDataInfo;
@synthesize itemListArray = _itemListArray;
@synthesize location = _location;
@synthesize wrongDataView = _wrongDataView;
@synthesize wrongDataImageView = _wrongDataImageView;
@synthesize wrongDataLabel = _wrongDataLabel;
@synthesize cityID = _cityID;
@synthesize typeName = _typeName;
@synthesize moreTypeTableView = _moreTypeTableView;
@synthesize hasLoadMoreType = _hasLoadMoreType;
@synthesize hasLoadBaseTable = _hasLoadBaseTable;
@synthesize requestMoreTypeID = _requestMoreTypeID;
@synthesize moreTypeID = _moreTypeID;
@synthesize topMoreDataModel = _topMoreDataModel;
@synthesize isMoreTableFont = _isMoreTableFont;

- (id)initWithFrame:(CGRect)frame andDelegate:(id<RCDiscountSubViewDelegate>)aDelegate andType:(NSString *)typeID andTypeName:(NSString *)typeName andLocation:(CLLocation *)currentLocation
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.baseTableView.delegate = self;
        self.baseTableView.dataSource = self;
        self.baseTableView.tag = kTableViewNormal;
        self.delegate = aDelegate;
        self.typeID = typeID;
        self.typeName = typeName;
        if ([typeName isEqualToString:@"更多排行"]) {
            self.moreTypeID = typeID;
            self.discountType = DiscountTypeMore;
        }
        self.isMoreTableFont = NO;
        self.hasLoadBaseTable = NO;
        self.hasLoadMoreType = NO;
        self.location = currentLocation;
        self.baseTableView.includeRefreshView = YES;
        if (!_itemListArray) {
            _itemListArray = [[NSMutableArray alloc]initWithCapacity:1];
        }
        if (self.discountType == DiscountTypeMore) {
            [self requestMoreTypeData];
        }else{
            [self requestDataList:NO];
        }
        
    }
    return self;
}

-(void)dealloc{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_requestID];
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_requestMoreTypeID];
    self.baseTableView.delegate = nil;
    self.baseTableView.dataSource = nil;
    self.delegate = nil;
    self.typeID = nil;
    self.typeName = nil;
    self.discountDataInfo = nil;
    self.itemListArray = nil;
    self.location = nil;
    self.wrongDataView = nil;
    self.wrongDataImageView = nil;
    self.wrongDataLabel = nil;
    self.moreTypeTableView.delegate = nil;
    self.moreTypeTableView.dataSource = nil;
    self.moreTypeTableView = nil;
    self.moreTypeID = nil;
    self.topMoreDataModel = nil;
    [super dealloc];
}

-(void)createWrongDataView{
    if (!_wrongDataView) {
        _wrongDataView = [[UIView alloc]initWithFrame:self.frame];
        _wrongDataView.backgroundColor = [UIColor clearColor];
        
        CGRect imageRect = CGRectMake(132, 0, 57, 57);
        imageRect.origin.y = self.frame.size.height - 249;
        CGFloat height = imageRect.origin.y + 57;
        
        _wrongDataImageView = [[UIImageView alloc]initWithFrame:imageRect];
        _wrongDataImageView.backgroundColor = [UIColor clearColor];
        [_wrongDataView addSubview:_wrongDataImageView];
        
        height +=20;
        
        _wrongDataLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, height, 320, 14)];
        _wrongDataLabel.backgroundColor = [UIColor clearColor];
        _wrongDataLabel.textAlignment = UITextAlignmentCenter;
        _wrongDataLabel.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:14];
        _wrongDataLabel.textColor = [UIColor colorWithHex:0xff717577];
        _wrongDataLabel.shadowColor = [UIColor colorWithHex:0xffffffff];
        _wrongDataLabel.shadowOffset = CGSizeMake(0, 1);
        [_wrongDataView addSubview:_wrongDataLabel];
        
        height +=26;
        
        
        UIButton *wrongDataButton = [[UIButton alloc]initWithFrame:CGRectMake(125, height, 70, 26)];
        NSString *normalImageString = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kNoNetworkButtonImage];
        NSString *pressImageString = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kNoNetworkPressButtonImage];
        UIImage *normalImage = [UIImage imageWithContentsOfFile:normalImageString];
        UIImage *pressImage = [UIImage imageWithContentsOfFile:pressImageString];
        [wrongDataButton setBackgroundImage:normalImage forState:UIControlStateNormal];
        [wrongDataButton setBackgroundImage:pressImage forState:UIControlStateHighlighted];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 7, 70, 13)];
        label.textAlignment = UITextAlignmentCenter;
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor colorWithHex:0xff4d5357];
        label.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:13];
        label.text = @"再试试";
        [wrongDataButton addSubview:label];
        [label release];
        
        [wrongDataButton addTarget:self action:@selector(noNetWorkButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [_wrongDataView addSubview:wrongDataButton];
        [wrongDataButton release];
        
        [self addSubview:_wrongDataView];
        _wrongDataView.hidden = YES;
    }
}

-(void)noNetWorkButtonClicked{
    NetworkStatus status = [GloableEngineService sharedInstance].netWorkStatus;
    if (kNotReachable == status) {
        [RCAppDelegate alertWithNetworkError];
        return;
    }
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CS_LocationFinished_Msg object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(locationFinished:)
                                                 name:CS_LocationFinished_Msg                                                   object:nil];
    if (nil == _location || _itemListArray == nil || [_itemListArray count] == 0) {
//        AppEngine.isShowLocationErrorMsg = YES;
        [[GloableEngineService sharedInstance]updataUserLocation];
    }else{
        [self requestDataList:YES];
    }
}


-(void)viewDidUnload{
    _currentOffset = 0;
    _currentReturnNumber = 0;
    _totalCount = 0;
    _isRefreshData = NO;
}

-(void)reloadData{
    [self.baseTableView reloadData];
}

-(void)clearDelegate{
    self.baseTableView.delegate = nil;
    self.baseTableView.dataSource = nil;
}

#pragma mark - Refresh delegate
- (void)refreshData //此接口必须被实现
{
    // 初始化页面计数
    currentPage = 0;
    
    // 判断网络状态
    if ([GloableEngineService sharedInstance].netWorkStatus == kNotReachable ){
        // 下拉/上拉操作数据获取完成后的处理
        [self refreshDataPostHandler];
        
        [RCAppDelegate alertWithNetworkError];
        return;
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CS_LocationFinished_Msg object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(locationFinished:)
                                                 name:CS_LocationFinished_Msg                                                   object:nil];
//    AppEngine.isShowLocationErrorMsg = NO;
    [[GloableEngineService sharedInstance]updataUserLocation];
    // 请求数据
}

- (void)locationFinished:(NSNotification *)notif
{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CS_LocationFinished_Msg object:nil];;
    
    if (1 == [(NSString*)[notif object] intValue])
    {
        [self updateLocationInfo:[GloableEngineService sharedInstance].locationInfo];
        [self requestDataList:YES];
    }
    else
    {
        if ([GloableEngineService sharedInstance].locationInfo)
        {//使用上一次
            [self updateLocationInfo:[GloableEngineService sharedInstance].locationInfo];
        }
        
        if (0 == [_itemListArray count])
        {
            //空，但是有gps，则获取信息
            [self requestDataList:YES];
        }
        else
        {
            [self refreshDataPostHandlerImmediately];
        }
    }
}

-(void)updateLocationInfo:(CLLocation *)changeLocation{
    if (nil == changeLocation) {
        return;
    }
    if (fabs(_location.coordinate.latitude - changeLocation.coordinate.latitude) >= kMinLatitudeOffset ||
        fabs(_location.coordinate.longitude - changeLocation.coordinate.longitude) >= kMinLongitudeOffset)
    {
        if (![_location isEqual:changeLocation])
        {
            [_location release];
            _location = nil;
            
            self.location = changeLocation;
            [self.itemListArray removeAllObjects];
            [self.baseTableView reloadData];
            _currentOffset = 0;
            _currentReturnNumber = 0;
        }
    }
}

- (void)loadMoreData //此接口必须被实现
{
    // 判断网络状态
    if ([GloableEngineService sharedInstance].netWorkStatus == kNotReachable ){
        // 下拉/上拉操作数据获取完成后的处理
        [self refreshDataPostHandler];
        
        [RCAppDelegate alertWithNetworkError];
        return;
    }
    //下拉刷新更新位置
    if ([GloableEngineService sharedInstance].locationInfo && !self.location) {
        [_location release];
        _location = nil;
        self.location = [GloableEngineService sharedInstance].locationInfo;
    }
    // 请求数据
    [self requestDataList:NO];
}

-(void)requestDataList:(BOOL)isRefreshData{
    
    NetworkStatus status = [GloableEngineService sharedInstance].netWorkStatus;
    BOOL dataExist = (nil == _itemListArray) || ([_itemListArray count]==0);
    if (status == kNotReachable && dataExist) {
        [self createWrongDataView];
        NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kNoNetWrokImageFile];
        UIImage *image = [UIImage imageWithContentsOfFile:filePath];
        [_wrongDataImageView setImage:image];
        _wrongDataLabel.text = @"没有感应到网络";
        self.baseTableView.hidden = YES;
        _wrongDataView.hidden = NO;
        return;
    }
    
    
    
    if (nil == _location && dataExist) {
        [self createWrongDataView];
        NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kNoLocationImageFile];
        UIImage *image = [UIImage imageWithContentsOfFile:filePath];
        [_wrongDataImageView setImage:image];
        
        if (![NCLocationController isLocationEnabled]) {
            _wrongDataLabel.text = @"没有获取到您的位置，请开启定位功能";
        }else{
            _wrongDataLabel.text = @"没有获取到您的位置，请再试试";
        }
        self.baseTableView.hidden = YES;
        _wrongDataView.hidden = NO;
        return;
    }
    
    _isRefreshData = isRefreshData;
    if (_delegate && [_delegate respondsToSelector:@selector(beginRequestData)]) {
        [_delegate beginRequestData];
    }
    
    CLLocationCoordinate2D bdMapLocation = CLLocationCoordinate2DMake(_location.coordinate.latitude, _location.coordinate.longitude);
    bdMapLocation = BMKCoorDictionaryDecode(BMKBaiduCoorForWgs84(bdMapLocation));
    
    NSString *map_x = [NSString stringWithFormat:@"%lf",bdMapLocation.longitude];
    NSString *map_y = [NSString stringWithFormat:@"%lf",bdMapLocation.latitude];
    
    self.cityID = [GloableEngineService sharedInstance].selectedCityID;
    if (isRefreshData) {
        
        _requestID = [[IManager sharedInstance]requestTopListDetailInfo:self.typeID withPn:0 withRn:[_itemListArray count] > 0 ? [_itemListArray count]:kMaxRequestCount withLat:map_y withLon:map_x withDelegate:self];
        
    }else{
        NSInteger offset = _currentOffset + _currentReturnNumber;

        _requestID = [[IManager sharedInstance]requestTopListDetailInfo:self.typeID withPn:offset withRn:kMaxRequestCount withLat:map_y withLon:map_x withDelegate:self];
    }
}

-(void)requestMoreTypeData{
    NetworkStatus status = [GloableEngineService sharedInstance].netWorkStatus;
    
    if (status == kNotReachable) {
        [self createWrongDataView];
        NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kNoNetWrokImageFile];
        UIImage *image = [UIImage imageWithContentsOfFile:filePath];
        [_wrongDataImageView setImage:image];
        _wrongDataLabel.text = @"没有感应到网络";
        self.baseTableView.hidden = YES;
        _wrongDataView.hidden = NO;
        return;
    }
    if (!_moreTypeTableView) {
        _moreTypeTableView = [[UITableView alloc]initWithFrame:self.bounds];
        _moreTypeTableView.backgroundColor = [UIColor clearColor];
        _moreTypeTableView.delegate = self;
        _moreTypeTableView.dataSource = self;
        _moreTypeTableView.tag = kTableViewMoreType;
        _moreTypeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:_moreTypeTableView];
    }
    
    if (!self.isMoreTableFont && self.hasLoadMoreType) {
        [UIView transitionFromView:self.baseTableView toView:self.moreTypeTableView duration:0.75 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
    }
    
    [self bringSubviewToFront:_moreTypeTableView];
    self.isMoreTableFont = YES;
    
    if (_delegate && [_delegate respondsToSelector:@selector(beginRequestData)]) {
        [_delegate beginRequestData];
    }
    _requestMoreTypeID = [[IManager sharedInstance]requestTopListMoreTypeInfo:self.moreTypeID withDelegate:self];
    
}

#pragma UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger tag = tableView.tag;
    if (tag == kTableViewMoreType) {
        return 36;
    }
    return 90;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger tag = tableView.tag;
    if (tag == kTableViewMoreType) {
        if (nil == self.topMoreDataModel || nil == self.topMoreDataModel.typeListArray) {
            return 0;
        }
        return [self.topMoreDataModel.typeListArray count];
    }
    return [_itemListArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger tag = tableView.tag;
    if (tag == kTableViewMoreType) {
        static NSString *morecellIdentifier = @"UITableViewMoreCell";
        RCPopViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:morecellIdentifier];
        if (nil == cell) {
            cell = [[[RCPopViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:morecellIdentifier]autorelease];
            
            cell.contentLabel.frame = CGRectMake(17, 14, 200, 16);
            cell.contentLabel.textColor = [UIColor colorWithHex:0xff505050];
            cell.contentLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
        }
        RCTopListTypeItem *item = [self.topMoreDataModel.typeListArray safeObjectAtIndex:[indexPath row]];
        
        [cell setcontentViewWithString:item.typeName];;
        
        return cell;
    }else{
        static NSString *cellIdentifier = @"UITableViewDiscountCell";
        
        RCMerchantListCell *discountCell = (RCMerchantListCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (nil == discountCell) {
            discountCell = [[[RCMerchantListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
            discountCell.accessoryType = UITableViewCellAccessoryNone;
            discountCell.selectionStyle = UITableViewCellSelectionStyleGray;
        }
        RCMerchant *itemInfo = [_itemListArray safeObjectAtIndex:[indexPath row]];
        if (nil == itemInfo) {
            return nil;
        }
        [discountCell bindData:itemInfo];
        
        return discountCell;
    }
    
    
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSInteger tag = tableView.tag;
    if (tag == kTableViewMoreType){
        RCTopListTypeItem *item = [self.topMoreDataModel.typeListArray safeObjectAtIndex:[indexPath row]];
        if (nil == item) {
            return;
        }
        self.typeID = item.typeID;
        [self requestDataList:YES];
        self.isMoreTableFont = NO;
        [UIView transitionFromView:self.moreTypeTableView toView:self.baseTableView duration:0.75 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil];
        [self bringSubviewToFront:self.baseTableView];
        return;
    }
    
    RCMerchant *itemInfo = [_itemListArray safeObjectAtIndex:[indexPath row]];
    if (nil == itemInfo) {
        return;
    }
    if (_delegate && [_delegate respondsToSelector:@selector(gotoDiscountChildView:WithChildViewType:)]) {
        [_delegate gotoDiscountChildView:itemInfo WithChildViewType:self.discountType];
    }
    
}

#pragma mark - Request delegate

- (void)updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    BOOL dataExist = (nil == _itemListArray) || ([_itemListArray count]==0);
    if (dataExist) {
        if (nil != _itemListArray && [_itemListArray count]!=0) {
            [_itemListArray removeAllObjects];
        }
        [self createWrongDataView];
        NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kNoLocationImageFile];
        UIImage *image = [UIImage imageWithContentsOfFile:filePath];
        [_wrongDataImageView setImage:image];
        _wrongDataLabel.text = @"没有找到周边信息";
        self.baseTableView.hidden = YES;
        _wrongDataView.hidden = NO;
    }
    //hide hub
    if (_requestID == requestId)
    {
        // 下拉/上拉操作数据获取完成后的处理
        [self refreshDataPostHandler];
        
        // 错误提示
        //        [AppDelegate alertWithCustomizeString:@"没有找到周边信息,换个地方试试!"];
        
        // 加载失败
        
    }
}

-(void)updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId{
    if (_requestID == requestId && requestId != kInvalidRequestId) {
        RCMerchantsListDataModel *discountDataModel = (RCMerchantsListDataModel *)dataModel;
        self.discountDataInfo = discountDataModel;
        if (_delegate && [_delegate respondsToSelector:@selector(requestFinishedData:)]) {
            [_delegate requestFinishedData:discountDataModel];
        }
        _currentReturnNumber = discountDataModel.rn;
        _currentOffset = discountDataModel.pn;
        _totalCount = discountDataModel.total;
        if (!_isRefreshData) {
            if (_currentReturnNumber > 0) {
                if (!_itemListArray) {
                    _itemListArray = [[NSMutableArray alloc]initWithCapacity:_currentReturnNumber];
                }
                
                //需要去重复
                NSMutableArray* originItemIdArr = [NSMutableArray array];
                
                for (RCMerchant* originLocalItemDataModel in self.itemListArray)
                {
                    [originItemIdArr addObject:originLocalItemDataModel.merchantID withDefaultValue:nil];
                }
                
                for (RCMerchant* localItemDataModel in discountDataModel.listItems)
                {
                    if (![originItemIdArr containsObject:localItemDataModel.merchantID])
                    {
                        [originItemIdArr addObject:localItemDataModel.merchantID
                                  withDefaultValue:nil];
                        [self.itemListArray addObject:localItemDataModel
                                     withDefaultValue:nil];
                    }
                }
                
            }
        }else{
            if (_currentReturnNumber > 0) {
                if (!_itemListArray) {
                    _itemListArray = [[NSMutableArray alloc]initWithCapacity:_currentReturnNumber];
                }
                [_itemListArray removeAllObjects];
                [_itemListArray addObjectsFromArray:discountDataModel.listItems];
            }
        }
        if (_wrongDataView && [self.itemListArray count] > 0)
        {
            _wrongDataView.hidden = YES;
        }
        
        if (0 == [self.itemListArray count])
        {
            if (!_wrongDataView)
            {
                [self createWrongDataView];
            }
            NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kNoLocationImageFile];
            UIImage *image = [UIImage imageWithContentsOfFile:filePath];
            [_wrongDataImageView setImage:image];
            _wrongDataLabel.text = @"没有找到周边信息";
            _wrongDataView.hidden = NO;
        }
        
        self.baseTableView.hidden = NO;
        [self reloadData];
    }
    
    //hide hub
    if (_requestID == requestId)
    {
        // 下拉/上拉操作数据获取完成后的处理
        [self refreshDataPostHandler];
        self.hasLoadBaseTable = YES;
    }else if (_requestMoreTypeID == requestId){
        
        RCTopListTypeMoreDataModel *moreDataModel = (RCTopListTypeMoreDataModel *)dataModel;
        self.topMoreDataModel = moreDataModel;
        
        if (_delegate && [_delegate respondsToSelector:@selector(endRequestData)]){
            
            [_delegate endRequestData];
        }
        self.hasLoadMoreType = YES;
        [self bringSubviewToFront:self.moreTypeTableView];
        [self.moreTypeTableView reloadData];
    }
    
    
    
}

- (void)refreshDataPostHandlerImmediately
{
    if ([_itemListArray count] >= _totalCount)
    {
        [self noMoreData];
    }
    else
    {
        isPullLoadMore = YES;
        [self addPullLoadMore];
    }
    
    // ”下拉刷新/上拉载入更多“的处理
    [self performSelector:@selector(finishRefreshingData) withObject:self afterDelay:0.1];
    [self performSelector:@selector(finishLoadMoreData) withObject:self afterDelay:0.1];
    
    if (_delegate && [_delegate respondsToSelector:@selector(endRequestData)]){
        
        [_delegate endRequestData];
    }
    
}

// 下拉/上拉操作数据获取完成后的处理
-(void)refreshDataPostHandler
{
    //    [self reloadData];
    
    if ([_itemListArray count] >= _totalCount)
    {
        [self noMoreData];
        self.baseTableView.includeLoadMoreView = NO;
    }
    else
    {
        isPullLoadMore = YES;
        self.baseTableView.includeLoadMoreView = YES;
        [self addPullLoadMore];
    }
    
    // ”下拉刷新/上拉载入更多“的处理
    [self performSelector:@selector(finishRefreshingData) withObject:self afterDelay:1];
    [self performSelector:@selector(finishLoadMoreData) withObject:self afterDelay:1];
    
    if (_delegate && [_delegate respondsToSelector:@selector(endRequestData)]){
        
        [_delegate endRequestData];
    }
    
}

-(void)switchTabbarButtonClicked{
    if (self.isMoreTableFont == NO && self.hasLoadMoreType) {
        [UIView transitionFromView:self.baseTableView toView:self.moreTypeTableView duration:0.75 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
        [self bringSubviewToFront:self.moreTypeTableView];
        self.isMoreTableFont = YES;
    }else if (self.isMoreTableFont && self.hasLoadBaseTable){
        [UIView transitionFromView:self.moreTypeTableView toView:self.baseTableView duration:0.75 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil];
        [self bringSubviewToFront:self.baseTableView];
        self.isMoreTableFont = NO;
    }
}

@end
